// 对useFetch进行简单的封装
const request = (url:string, load:any) => {
    return new Promise<any>((res, rej) => {
        const baseURL = (load.server === false && process.env.NODE_ENV === 'development') ?
            useRuntimeConfig().public.baseURL : useRuntimeConfig().public.baseServerURL
        console.log(baseURL)
        console.log(url)
        useFetch(url, {
            ...load,
            immediate: !(load.server === false),
            baseURL: baseURL,
            onRequest({ options }) {
                options.headers = options.headers || {}
                // options.headers.token = '233'
            },
            onRequestError() {
                // 处理请求错误
            },
            onResponse() {
                // 处理响应数据
            },
            onResponseError() {
                // 处理响应错误
            }
        }).then(async ({ data, pending, error, refresh }) => {
            // 如果为客户端渲染，等待水合完毕后刷新
            if (load.server === false) {
                await refresh()
            }
            const code = data.value ? JSON.parse(data.value as string).status : 400
            switch (code) {
                case 1:
                    data = JSON.parse(data.value as string)
                    res({ data, pending, error, refresh })
                    break;
                case 10020:
                    rej(Promise.reject({ data, pending, error, refresh }))
                    break;
                case 10021:
                    rej(Promise.reject({ data, pending, error, refresh }))
                    break;
                default:
                    return rej(Promise.reject({ data, pending, error, refresh }))
            }
        })
    })
}

export default request